<template>
	<view>
		<u-navbar   :is-back="true" title="申请添加朋友" :title-bold="true" :title-size="34"
					:background="{ background: '#ffffff'  }"
					title-color="#404133" :border-bottom="false">
		</u-navbar>
		
		<view class="">
			<scroll-view :scroll-x="false" :scroll-y="true" class="u-p-t-10 u-p-b-30"
				:style="'width:100%;height:'+scrollviewHeight+'px'">
				<view class="u-p-l-40 u-p-r-40">
					<view class="u-p-l-20 u-tips-color u-font-28">
						<text>发送添加朋友申请</text>
					</view>
					<view  class="inputBox">
						<u-input :custom-style="customInputStyle"  v-model="form.applyUserName" type="textarea" :border="false" :height="150" :clearable="false"/>
					</view>
					<view class="u-tips-color u-p-l-30 u-m-t-10">
						<text style="color: #36648b;" v-if="form.applyUserName!=usuallyTip">设为常用申请语</text>
						<text v-else>常用申请语:“{{usuallyTip}}”<text style="color: #36648b;" >填入</text></text>
					</view>
				</view>
				
				<view class="u-m-t-30 u-p-l-40 u-p-r-40">
					<view class="u-p-l-20 u-tips-color u-font-28">
						<text>设置备注</text>
					</view>
					<view  class="inputBox" style="">
						<u-input :placeholder="friendNameTip"  :custom-style="customInputStyle" 
								placeholder-style="font-size:32rpx" @focus="friendNameFocus()"
								v-model="form.friendName" type="text" :border="false" 
								:clearable="vuex_OSPlat=='android'?false:true"/>
					</view>
				</view>
				
				<view class="u-m-t-30 u-p-l-40 u-p-r-40">
					<view class="u-p-l-20 u-tips-color u-font-28">
						<text>添加标签与描述</text>
					</view>
					<view style="background-color: #f0f0f0;border-radius: 16rpx;margin-top: 20rpx;font-size: 32rpx;">
						<view class="u-flex u-row-between u-col-center" style="padding:16rpx 26rpx;">
							<view class="u-p-20">
								<text>标签</text>
							</view>
							<view class="u-p-20">
								<u-icon name="arrow-right" color="#909399" :size="32"></u-icon>
							</view>
						</view>
						<u-gap :height="1" bg-color="#e5e8ec"></u-gap>
						<view class="u-flex u-row-between u-col-center"  style="padding:16rpx 26rpx;">
							<view class="u-p-20">
								<text>描述</text>
							</view>
							<view  class="u-p-20">
								<u-icon name="arrow-right" color="#909399" :size="32"></u-icon>
							</view>
						</view>
					</view>
				</view>
				
				<view class="u-m-t-30 u-p-l-40 u-p-r-40">
					<view class="u-p-l-20 u-tips-color u-font-28">
						<text>设置朋友权限</text>
					</view>
					<view style="background-color: #f0f0f0;border-radius: 16rpx;margin-top: 20rpx;font-size: 32rpx;">
						<view class="u-flex u-row-between u-col-center" 
							 @click="changePower(0)"
							style="padding:16rpx 26rpx;">
							<view class="u-p-20">
								<text>聊天、朋友圈、微信运动等</text>
							</view>
							<view class="u-p-20" v-show="form.friendPower==0">
								<u-icon name="checkbox-mark" color="#19be6b" :size="32"></u-icon>
							</view>
						</view>
						<u-gap :height="1" bg-color="#e5e8ec"></u-gap>
						<view class="u-flex u-row-between u-col-center" 
							  @click="changePower(1)"
							  style="padding:16rpx 26rpx;">
							<view class="u-p-20">
								<text>仅聊天</text>
							</view>
							<view  class="u-p-20" v-show="form.friendPower==1">
								<u-icon name="checkbox-mark" color="#19be6b" :size="32"></u-icon>
							</view>
						</view>
					</view>
				</view>
				
				<view class="u-m-t-30 u-p-l-40 u-p-r-40" v-if="form.friendPower==0">
					<view class="u-p-l-20 u-tips-color u-font-28">
						<text>朋友圈和状态</text>
					</view>
					<view style="background-color: #f0f0f0;border-radius: 16rpx;margin-top: 20rpx;font-size: 32rpx;">
						<view class="u-flex u-row-between u-col-center" style="padding:16rpx 26rpx;">
							<view class="u-p-20">
								<text>不让她看我</text>
							</view>
							<view class="u-p-20">
								<u-switch v-model="form.forbidSelf"  active-color="#19be6b" inactive-color="#eee"></u-switch>
							</view>
						</view>
						<u-gap :height="1" bg-color="#e5e8ec"></u-gap>
						<view class="u-flex u-row-between u-col-center"  style="padding:16rpx 26rpx;">
							<view class="u-p-20">
								<text>不看他</text>
							</view>
							<view  class="u-p-20">
								<u-switch v-model="form.forbidHis"  active-color="#19be6b" inactive-color="#eee"></u-switch>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view :style="'height:'+footerHeight+'px'"
				class="u-flex u-row-center u-col-top footerbox">
			<u-button type="success" :custom-style="customBtnStyle" @click="submitApply">发送</u-button>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				scrollviewHeight:0,
				footerHeight:0,
				backPageDelta:2,
				newFriendInfo:{},
				customInputStyle:{
					fontSize:'32rpx',
					color:'#000000'	
				},
				form:{
					applyUserId:'',
					applyUserName: '',
					friendName:'',
					friendTag:'',
					friendPower:0,
					forbidSelf:false,
					forbidHis:false
				},
				usuallyTip:'',
				friendNameTip:'',
				customBtnStyle:{
					padding:'20rpx 40rpx',
					width:'300rpx'
				}
			};
		},
		created:function(){
			let pageHeight= this.$u.sys().windowHeight;
			this.scrollviewHeight=pageHeight*0.783;
			this.footerHeight=pageHeight*0.09;
		},
		onLoad:function(option){
			let that=this;
			let fromPage= option.fromPage;
			if(fromPage!=null&&fromPage!=undefined){
				that.backPageDelta=fromPage;
			}
			that.newFriendInfo=JSON.parse(decodeURIComponent(option.newFriendInfo));
			console.log("新的朋友",that.newFriendInfo);
			//this.form.friendName=that.newFriendInfo.userName;
			this.friendNameTip=that.newFriendInfo.userName;
			this.form.applyUserName="我是"+that.vuex_userInfo.userName;
			this.form.applyUserId=that.vuex_userInfo.id;
			this.usuallyTip="我是"+that.vuex_userInfo.userName;
		},
		methods:{
			friendNameFocus:function(){
				let that=this;
				if(this.form.friendName.length<1){
					this.form.friendName=that.newFriendInfo.userName;
					this.friendNameTip="";
				}
			},
			changePower:function(status){
				this.form.friendPower=status;
			},
			submitApply:function(){
				let that=this;
				let param={
					friendId:that.newFriendInfo.id,
					friendName:that.form.friendName.length<1?that.newFriendInfo.userName:that.form.friendName,
					friendTag:that.form.friendTag,
					friendMobile:that.newFriendInfo.mobile,
					friendPower:that.form.friendPower,
					description:that.form.applyUserName,
				};
				console.log("param",param);
				that.$u.api.imUser.addFriend(param).then(res => {
					if(res.code==200){
					   console.log("申请添加好友结果",res);
					   that.sendFriendApplyNotice(param);
					  // uni.navigateBack();
					}
				})
			},
			
			sendFriendApplyNotice:function(messageContent){
				let that=this;
				let timestamp=new Date().getTime();
				let message = {
					"id": new Date().getTime() + "",
					"platFrom": that.messageApi.msgPlat,
					"userId":that.vuex_userInfo.id,
					"userName":that.vuex_userInfo.userName,
					"messageType":that.messageApi.MSG_TYPE.SYSTEM_MSG,
					"contentType":that.messageApi.CONTENT_TYPE.TEXT_CONTENT_TYPE,
					"createTime": that.globalUtil.getTimeStr(timestamp),
					"timestamp": timestamp,
					"content":messageContent,
				};
				this.globalWX.sendMessage(message, {
					complete: function(res) {
						if(res==true){
							that.globalUtil.utilAlert("已发送申请,待对方同意");
							uni.navigateBack({
								delta:3
							})
						}else{
							that.globalUtil.utilAlert("网络故障，稍后重试！");
							return;
						}
					}
				},that.messageApi.BUSINESS_CODE.FriendBindChange);
			},
		}
	}
</script>
<style lang="scss" scoped>
	.inputBox{
		background-color: #f0f0f0;border-radius: 16rpx;margin-top: 20rpx;padding:16rpx 26rpx;font-size: 32rpx;
	}
	
	.footerbox{
		position: absolute;
		bottom: 0px;
		width: 100%;
		background-color: #ffffff;
		//opacity: 0.7;
	}
</style>
<style lang="scss">
page{
	background-color: #ffffff;
}
</style>
